package com.example.wuyeguanli.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ActionBar;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
import android.widget.TextView;

import com.example.wuyeguanli.R;

public class MainActivity extends AppCompatActivity {

    Button btn1;
    Button btn2;
    Button btn3;
    Button btn4;
    Button btn5;
    Button btn6;
    Button btn8;
    TextView tv_username;
    SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //隐藏标题栏
        if(getSupportActionBar() != null){
            getSupportActionBar().hide();
        }

        btn1 = findViewById(R.id.btn_1);
        btn2 = findViewById(R.id.btn_2);
        btn3 = findViewById(R.id.btn_3);
        btn4 = findViewById(R.id.btn_4);
        btn5 = findViewById(R.id.btn_5);
        btn6 = findViewById(R.id.btn_6);
        btn8 = findViewById(R.id.btn_8);
        tv_username = findViewById(R.id.tv_username);
        sharedPreferences = getSharedPreferences("user", MODE_PRIVATE);

        // 报修申请
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到报修列表页面
                Intent intent = new Intent(MainActivity.this, RepairlistActivity.class);
                startActivity(intent);
            }
        });
//////废除/////
//        // 维修进度查询
//        Button btnRepairProgress = findViewById(R.id.btn_repair_progress);
//        btnRepairProgress.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                // 跳转到维修进度查询页面
//                Intent intent = new Intent(MainActivity.this, RepairProgressActivity.class);
//                startActivity(intent);
//            }
//        });
/////废除/////
        // 缴费查询
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到缴费查询页面
                Intent intent = new Intent(MainActivity.this, FeeQueryActivity.class);
                startActivity(intent);
            }
        });

        // 公告查看
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到公告查看页面
                Intent intent = new Intent(MainActivity.this, AnnouncementActivity.class);
                startActivity(intent);
            }
        });

        // 投诉建议
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到投诉建议页面
                Intent intent = new Intent(MainActivity.this, ComplaintListActivity.class);
                startActivity(intent);
            }
        });

        // 访客管理
        btn5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到访客管理页面
                Intent intent = new Intent(MainActivity.this, VisitorlistActivity.class);
                startActivity(intent);
            }
        });

        // 设施预订
        btn6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到设施预订页面
                Intent intent = new Intent(MainActivity.this, FacilityBookingActivity.class);
                startActivity(intent);
            }
        });

        btn8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putBoolean("login", false);
                editor.remove("username");
                editor.apply();

                Intent intent = new Intent(MainActivity.this, RegisterLoginActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        // 住户注册/登录
        if (!sharedPreferences.getBoolean("login", false)) {
            Intent intent = new Intent(MainActivity.this, RegisterLoginActivity.class);
            startActivity(intent);
            finish();
        }
        tv_username.setText(String.format("欢迎，%s", sharedPreferences.getString("username", "用户名")));
    }
}